package services

import (
	"gitee.com/zhengjianfeng/pay/common"
	"gitee.com/zhengjianfeng/pay/dao"
	"gitee.com/zhengjianfeng/pay/datasource"
	"gitee.com/zhengjianfeng/pay/models"
	"time"
)

type PayAccountService interface {
	GetOne(username string) *models.PayAccount
	GetSave(data *models.PayAccount) error
	CreateAccount(username string, nickname string, pwd string, roleType int, relevantId int64) (accountId int64, err error)
}
type payAccountService struct {
	payAccountDao *dao.PayAccountDao
}

func NewPayAccountService() *payAccountService {
	return &payAccountService{
		payAccountDao: dao.NewPayAccountDao(datasource.InstanceMaster()),
	}
}

func (s *payAccountService) GetOne(username string) *models.PayAccount {
	return s.payAccountDao.GetOne(username)
}

func (s *payAccountService) GetSave(data *models.PayAccount) (err error) {
	err = s.payAccountDao.GetSave(data)
	return
}

// 创建账号
func (s *payAccountService) CreateAccount(username string, nickname string, pwd string, roleType int, relevantId int64) (accountId int64, err error) {
	var (
		createData *models.PayAccount
		now        time.Time
	)

	now = time.Now()

	createData = &models.PayAccount{
		Username:   username,
		Nickname:   nickname,
		Password:   common.EncryptionForAccount(pwd, username),
		RoleType:   roleType,                       // 管理员
		RelevantId: relevantId,                     // 相关ID
		Status:     common.PAY_ACCOUNT_STATUS_OPEN, // 开启
		GmtModify:  now,
		GmtCreate:  now,
	}

	err = s.GetSave(createData)

	accountId = createData.Id

	return
}
